<?php
class Subscribe_Push_Strategy_Glt implements Subscribe_Push_Strategy
{
	private $_style;
	private $_userListObj;
	private $_change;
	
	public function setStyle($style)
	{
		$this->_style = $style;
	}
	
	public function setUserList(Subscribe_Push_UserList $userListObj)
	{
		$this->_userListObj = $userListObj;
	}
	
	public function setMessage($change)
	{
		$this->_change = $change;
	}
	
	public function getMessage()
	{
		$code = $this->_change['code'];
		$header = '尊敬的大战略用户：大战略';
		$type = ($this->_style == 0) ? '短线强势股票池' : '理念股票池';
		$jr = '今日';
		$rxdgp = '入选的股票';
		$jjdd = '即将到达';
		$yjdd = '已经到达';
		$price = $this->_change['pointPrice'];
		$footer = '元，敬请关注。同花顺提醒您，股市有风险，';
		if ($this->_change['type'] == 'premr') {
			$date = $jr;
			$toPoint = $jjdd . '买入价';
			$announce = '入市须谨慎！';
		} elseif ($this->_change['type'] == 'mr') {
			$date = $jr;
			$toPoint = $yjdd . '买入价';
			$announce = '入市须谨慎！';
		} elseif ($this->_change['type'] == 'prezs') {
			$date = date('m月d日', strtotime($this->_change['date']));
			$toPoint = $jjdd . '止损价';
			$announce = '止损须及时！';
		} elseif ($this->_change['type'] == 'zs') {
			$date = date('m月d日', strtotime($this->_change['date']));
			$toPoint = $yjdd . '止损价';
			$announce = '止损须及时！';
		} elseif ($this->_change['type'] == 'prezy') {
			$date = date('m月d日', strtotime($this->_change['date']));
			$toPoint = $jjdd . '目标价';
			$announce = '落袋方为安！';
		} elseif ($this->_change['type'] == 'zy') {
			$date = date('m月d日', strtotime($this->_change['date']));
			$toPoint = $yjdd . '目标价';
			$announce = '落袋方为安！';
		}
		$message = $header . $type . $date . $rxdgp . $stockName . '(' . $code . ')' 
			. $toPoint . $price . $footer . $announce;
		return $message;
	}
	
	private function _setGetParem(array $params)
	{
		$gets = '';
		$i = 0;
		foreach ($params as $paramName => $paramValue) {
			if ($paramName == 'isInRange') {
				continue;
			}
			if ($paramName == 'type') {
				$paramName = 't';
			}
			if ($paramName == 'code') {
				$paramName = 'c';
			}
			if ($paramName == 'currentPrice') {
				continue;
			}
			if ($paramName == 'date') {
				$paramName = 'd';
			}
			if ($paramName == 'pointPrice') {
				$paramName = 'pp';
			}
			if ($i == 0) {
				$gets .= '?' . $paramName . '=' . $paramValue;
				$i++;
			} else {
				$gets .= '&' . $paramName . '=' . $paramValue;
			}
		}
		return $gets;
	}
	
	public function push()
	{
		echo $this->getMessage();
		foreach ($this->_userListObj->getUserList() as $user) {
			echo "\n发送给用户：" . $user['username'] . " ";
			$gltObj = new Subscribe_Push_Glt_WebMessage();
			$domain = "http://vis.10jqka.com.cn/dzl/push.php";
			$url = $domain . $this->_setGetParem($this->_change) . "&s=" 
				. $this->_style . '#title=大战略股价预警';
			
			echo $url;
			$endTime = strtotime(date('Y-m-d 23:59:59'));
			$gltObj->setUrl($url);
			$gltObj->setTo($user['msgcode']);
			$gltObj->setEndtime($endTime);
			$gltObj->setWidth(420);
			$gltObj->setHeight(300);
			$gltObj->setYwid(66); //大战略的业务id是66
			$gltObj->setFuncid(1); //大战略的功能id是1 
			$rtn = $gltObj->sendMessage();
			if ($rtn->getResult() == 1) {
				echo "success\n";
			} else {
				echo "failed! " . $rtn->getResultMsg() . "!\n";
			}
		}
	}
	
	private function _setStockName()
	{
		$code = $this->_change['code'];
		if (Hexin_AStock::isAStock($code)) {
			$stock = Hexin_AStock::find($code);
			$this->_change['stockname'] = $stock->getStockName();
		} else {
			$this->_change['stockname'] = null;
		}
	}
}